﻿<?php
function getIp() { 
    if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) $ip = getenv("HTTP_CLIENT_IP"); 
    else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) $ip = getenv("HTTP_X_FORWARDED_FOR"); 
    else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) $ip = getenv("REMOTE_ADDR"); 
    else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) $ip = $_SERVER['REMOTE_ADDR']; 
    else $ip = "unknown"; 
    return ($ip); 
} 
function getFile($uploadedfile,$userurl,$myimg,$type){
	// type==1 单张图片
	// type==2 多张图片
	$myimg->img_url="";//统一数据库的格式
	if($type==1){
		$target_path = "../upload/".$userurl."/";
		if(!is_dir($target_path)){
			$res=mkdir(iconv('utf-8', 'gbk', $target_path));
			if ($res){
					

			}else{
				$myimg->result="FALSE";
				return;
			}
		}
		if(isset($_FILES[$uploadedfile])){
			$oldname=$_FILES[$uploadedfile]['name'];
			$date=date('Ymdhis')."_".$oldname;
			$target_path = $target_path.$date;
			if(move_uploaded_file($_FILES[$uploadedfile]['tmp_name'], $target_path)){
				$myimg->result="TRUE";
				$myimg->img_url=$target_path;
			}else{
				$myimg->result="FALSE";
			}
				
		}
		else{
			$myimg->result="FALSE";
		}
	}else{
		$target_path = "../upload/".$userurl."/";
		if($type==3){
			$target_path = "../plugin/".$userurl."/";
		}
		if(!is_dir($target_path)){
			$res=mkdir(iconv('utf-8', 'gbk', $target_path));
			if ($res){
					

			}else{
				$myimg->result="FALSE";
				return;
			}
		}
		foreach($_FILES[$uploadedfile]['name'] as $index =>$name){
				//修改图片名
			if(isset($_FILES[$uploadedfile])){
				$oldname=$_FILES[$uploadedfile]['name'][$index];
				$date=date('Ymdhis')."_".$oldname;
				if($type==3){
					$date=$myimg->vision."_".$oldname;
				}
				$target_path = $target_path.$date;
				if($oldname==""){
					$myimg->result="TRUE";
					return;
				}
				if(move_uploaded_file($_FILES[$uploadedfile]['tmp_name'][$index], $target_path)){
					$myimg->result="TRUE";
					$myimg->img_url=$myimg->img_url.$target_path.";";
				}else{
					$myimg->result="FALSE";
				}
				
			}
			else{
				$myimg->result="FALSE";
			}
		}
	}
}

// foreach($_FILES['uploadedfile']['name'] as $index =>$name){
//  	$tmp = explode('.',$name); //explode 函数
//     $docName = time().rand(1,1000).".".$tmp[1];
// 	$uploadfile =  "../upload/".$docName;
// 	if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$index],$uploadfile)){
// 		echo "文件".$name."上传成功<br/>";
// 	}else{
// 		echo "文件".$name."上传失败<br/>";
// 	}
//  }
function hobby_type_insert($mydb,$type,$user_id,$type_name){
	$result = TRUE;
	if($type!=""){
		$type=explode(",",$type);
		foreach ($type as $value) {
			$param="select * from yj_user_hobby where user_id ='".$user_id."' and user_hobby='".$value."'";
			$result1 = mysqli_num_rows($mydb->exec_query($param));
			if($result1==0){
				$param="INSERT INTO yj_user_hobby (user_id,hobby_type,user_hobby) VALUES('".$user_id."','".$type_name."','".$value."')";
				$result2=$mydb->exec_query($param);
				if($result2==TRUE){
					$result=TRUE;
				}
				else{
					$result=FALSE;
				}
			}else{
				
			}
		}
	}
	return $result;
}
function hobby_insert($mydb,$json_hobby,$user_id){
		$sport=$json_hobby->sport;
		$tech=$json_hobby->tech;
		$skin=$json_hobby->skin;
		$game=$json_hobby->game;
		$result=FALSE;
		$result1=hobby_type_insert($mydb,$sport,$user_id,"sport");
		$result2=hobby_type_insert($mydb,$tech,$user_id,"tech");
		$result3=hobby_type_insert($mydb,$skin,$user_id,"skin");
		$result4=hobby_type_insert($mydb,$game,$user_id,"game");
		if($result1 && $result2 && $result3 && $result4){
			$result=TRUE;
		}
		return $result;
	}

function jsonFormat($data, $indent=null){  
  
    // 对数组中每个元素递归进行urlencode操作，保护中文字符  
    array_walk_recursive($data, 'jsonFormatProtect');  
  
    // json encode  
    $data = json_encode($data);  
  
    // 将urlencode的内容进行urldecode  
    $data = urldecode($data);  
  
    // 缩进处理  
    $ret = '';  
    $pos = 0;  
    $length = strlen($data);  
    $indent = isset($indent)? $indent : '    ';  
    $newline = "\n";  
    $prevchar = '';  
    $outofquotes = true;  
  
    for($i=0; $i<=$length; $i++){  
  
        $char = substr($data, $i, 1);  
  
        if($char=='"' && $prevchar!='\\'){  
            $outofquotes = !$outofquotes;  
        }elseif(($char=='}' || $char==']') && $outofquotes){  
            $ret .= $newline;  
            $pos --;  
            for($j=0; $j<$pos; $j++){  
                $ret .= $indent;  
            }  
        }  
  
        $ret .= $char;  
          
        if(($char==',' || $char=='{' || $char=='[') && $outofquotes){  
            $ret .= $newline;  
            if($char=='{' || $char=='['){  
                $pos ++;  
            }  
  
            for($j=0; $j<$pos; $j++){  
                $ret .= $indent;  
            }  
        }  
  
        $prevchar = $char;  
    }  
  
    return $ret;  
}  
function getFirstCharter($str){
	if(empty($str)){return '';}
	$fchar=ord($str{0});
	if($fchar>=ord('A')&&$fchar<=ord('z')) return strtoupper($str{0});
	$s1=iconv('UTF-8','gb2312',$str);
	$s2=iconv('gb2312','UTF-8',$s1);
	$s=$s2==$str?$s1:$str;
	$asc=ord($s{0})*256+ord($s{1})-65536;
	if($asc>=-20319&&$asc<=-20284) return 'A';
	if($asc>=-20283&&$asc<=-19776) return 'B';
	if($asc>=-19775&&$asc<=-19219) return 'C';
	if($asc>=-19218&&$asc<=-18711) return 'D';
	if($asc>=-18710&&$asc<=-18527) return 'E';
	if($asc>=-18526&&$asc<=-18240) return 'F';
	if($asc>=-18239&&$asc<=-17923) return 'G';
	if($asc>=-17922&&$asc<=-17418) return 'H';
	if($asc>=-17417&&$asc<=-16475) return 'J';
	if($asc>=-16474&&$asc<=-16213) return 'K';
	if($asc>=-16212&&$asc<=-15641) return 'L';
	if($asc>=-15640&&$asc<=-15166) return 'M';
	if($asc>=-15165&&$asc<=-14923) return 'N';
	if($asc>=-14922&&$asc<=-14915) return 'O';
	if($asc>=-14914&&$asc<=-14631) return 'P';
	if($asc>=-14630&&$asc<=-14150) return 'Q';
	if($asc>=-14149&&$asc<=-14091) return 'R';
	if($asc>=-14090&&$asc<=-13319) return 'S';
	if($asc>=-13318&&$asc<=-12839) return 'T';
	if($asc>=-12838&&$asc<=-12557) return 'W';
	if($asc>=-12556&&$asc<=-11848) return 'X';
	if($asc>=-11847&&$asc<=-11056) return 'Y';
	if($asc>=-11055&&$asc<=-10247) return 'Z';
	return null;
}

// 敏感词过滤函数
function sensitivity_words_test($tweetsid,$mydb,$str){
	$param="select * from yj_sensitive_words";
	$result=$mydb->exec_query($param);
	if($result==TRUE){
		while ($row = mysqli_fetch_array($result,MYSQL_ASSOC)) {
			if (substr_count ( $str, $row['sensitivity_value'] ) > 0) {  
             	$param="INSERT INTO yj_sensitivity_record(sensitivity_id,tweets_id) VALUES ('".$row['sensitivity_id']."','".$tweetsid."')";
        		$result=$mydb->exec_query($param);
        		if($result==TRUE){
        			continue;
        		}
        	}
		}
	}
}
?>